// 组件开发
// http://doc.go-admin.cn/zh/development/template/components/

package cherryeditor

import (
	_ "embed"
	"html/template"
	"path/filepath"

	adminTemplate "github.com/GoAdminGroup/go-admin/template"
)

//go:embed cherry_editor.tmpl
var htmlData string

//go:embed assets/cherry-markdown.min.css
var css []byte

//go:embed assets/cherry-markdown.min.js
var js []byte

// //go:embed assets/fonts/ch-icon.woff2
// var woff2 []byte

// //go:embed assets/fonts/ch-icon.woff
// var woff []byte

// //go:embed assets/fonts/ch-icon.ttf
// var ttf []byte

type CheeryEditor struct {
	*adminTemplate.BaseComponent
}

func New() *CheeryEditor {
	return &CheeryEditor{
		BaseComponent: &adminTemplate.BaseComponent{
			Name:     "cherry_editor",
			HTMLData: htmlData,
		},
	}
}

func (l *CheeryEditor) GetAssetList() []string {
	return []string{
		"/cherry-markdown.min.css",
		"/cherry-markdown.min.js",
		// "/fonts/ch-icon.ttf",
		// "/fonts/ch-icon.woff",
		// "/fonts/ch-icon.woff2",
	}
}

func (l *CheeryEditor) GetAsset(name string) ([]byte, error) {
	// return os.ReadFile("."+name)
	// println("ext:", filepath.Ext(name))
	switch filepath.Ext(name) {
	case ".css":
		return css, nil
	case ".js":
		return js, nil
	// case ".woff2":
	// 	return woff2, nil
	// case ".woff":
	// 	return woff, nil
	// case ".ttf":
	// 	return ttf, nil
	default:
		return []byte{}, nil
	}
}

func (l *CheeryEditor) GetContent() template.HTML {
	// buffer := new(bytes.Buffer)
	// tmpl, defineName := l.GetTemplate()
	// err := tmpl.ExecuteTemplate(buffer, defineName, l)
	// if err != nil {
	// 	println("cherry editor, compose html error:", err)
	// }
	// return template.HTML(buffer.String())

	// 不能编译模版文件，否则 FormField 无法传递到模版文件
	// http://doc.go-admin.cn/zh/admin/form/components/
	// FieldCustomContent 会将 FormField 传递到模版文件并编译
	return template.HTML(l.HTMLData)
}
